package com.situ.feemanagement.controller;

import com.situ.feemanagement.pojo.entity.Medicine;
import com.situ.feemanagement.pojo.query.MedicineQuery;
import com.situ.feemanagement.pojo.vo.QuantityVO;
import com.situ.feemanagement.service.IMedicineService;
import com.situ.feemanagement.util.Result;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/medicine")
public class MedicineController {

    @Autowired
    private IMedicineService medicineService;

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Medicine> list = medicineService.selectAll();
        return Result.ok(list);
    }

    @GetMapping("/list")
    public Result list(MedicineQuery medicineQuery) {
        Map<String,Object> map = medicineService.selectByPage(medicineQuery);
        return Result.ok(map);
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        medicineService.deleteById(id);
        return Result.ok("删除成功");
    }

    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        medicineService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Medicine medicine) {
        medicineService.add(medicine);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Medicine medicine = medicineService.selectById(id);
        return Result.ok(medicine);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Medicine medicine) {
        medicineService.update(medicine);
        return Result.ok("更新成功");
    }

    @GetMapping("/selectQuantity/{medicineId}")
    public Result selectQuantity(@PathVariable Integer medicineId) {
        List<QuantityVO> list = medicineService.selectQuantity(medicineId);
        return Result.ok(list);
    }

    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        medicineService.exportExcel(response);
    }

    @RequestMapping("/importExcel")
    public Result importExcel(MultipartFile file) {
        medicineService.importExcel(file);
        return Result.ok();
    }
}
